/**
 * Created by xiangyufang on 2016/11/10.
 */
;(function ($, window, document, undefined) {
    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });

    //相对位置关系
    var $position = $('#JQ_position').select3({
        data:[
            {id:'CROSS',text:'交叉'},
            {id:'PARALLEL',text:'平行'}
        ]
    });

    //表格
    var $table = $("#JQ_table");
    //查询
    var $search = $("#JQ_list_search");
    //地图查询
    var $mapSearch = $("#JQ_map_search");
    //新建
    var $create = $("#JQ_create");
    //导出
    var $export = $("#JQ_export");
    //切换地图
    var $toggle_map = $("#JQ_toggle_map_span");
    //切换表格
    var $toggle_list = $("#JQ_toggle_list_span");



    // 查询对象
    var filter = {
        getSearch: function (param) {
            var data = {};
            if (param) {
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            //管道参数
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            //管道名称
            data.pipeLineName = function(){
                var v = $pipeline.val('text');
                if(v){
                    return v;
                }
                return "";
            }();
            //起点参数
            data.startMileage = function () {
                var v = $begin.val('mile');
                if (v) {
                    return v;
                }else if(v == 0){
                    return v;
                }
                return "";
            }();
            //终点参数
            data.endMileage = function () {
                var v = $end.val('mile');
                if (v) {
                    return v;
                }else if(v == 0){
                    return v;
                }
                return "";
            }();
            //相对位置关系
            data.relativePositionRelation = function () {
                var v = $position.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            return data;
        },
        // 获得导出条件
        getExport: function(){
            return filter.getSearch();
        }
    };


    // 加载表格
     var DTTABLE = $table.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch(data);
            DyyBase.ajax(ambientApi.url_ae_pipe_search(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
            var th = $table.find('th');
            var empty = $table.find('td.dataTables_empty');
            empty.attr('colspan',th.length);
        },
        'columns': [
            {
                data: 'pipeLineName',
                render: function (data,type,row) {
                    return '<span title="' + data + '">' + data + '</span><input type="hidden" name="gisId" value="' + row.gisid + '">';
                }
            },
            {
                data: 'type',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'owner',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'relativePositionRelation',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'relativePositionRelation',
                render: function (data,type,row) {
                    if(data == '交叉'){
                        return '<span title="' + row.crossNagm + '">' + row.crossNagm + '</span>';
                    }else  if(data == '平行'){
                        return '<span title="' + row.beginNagm + '">' + row.beginNagm + '</span>';
                    }
                }
            },
            {
                data: 'safeHeight',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'id',
                render: function (data,type,row) {
                    var state = row.dataStated;
                    if(state == '20'){
                        return '<i data-a="' + data + '" class="xyf-shenhe-no-icon"></i>';
                    }else if(state == '05'){
                        return '<i data-a="' + data + '" class="zy-list-icon-examine" title="审核"></i>';
                    }else if(state == '06'){
                        return '<i data-a="' + data + '" class="zy-shenhe-no-icon"></i>';
                    }
                }
            },
            {
                data: 'id',
                render: function (data,type,row) {
                    var state = row.dataStated;
                    if(state == '20'){
                        return '<i data-a="' + data + '" class="zy-record-add-icon disabled"></i>';
                    }else if(state == '05'){
                        return '<i data-a="' + data + '" class="zy-record-add-icon" title="编辑"></i>';
                    }else if(state == '06'){
                        return '<i data-a="' + data + '" class="zy-record-add-icon" title="编辑"></i>';
                    }
                }
            },
            {
                data: 'id',
                render: function (data,type,row) {
                    var state = row.dataStated;
                    if(state == '20'){
                        return '<i data-a="' + data + '" class="zy-list-icon-delete disabled"></i>';
                    }else if(state == '05'){
                        return '<i data-a="' + data + '" class="zy-list-icon-delete" title="删除"></i>';
                    }else if(state == '06'){
                        return '<i data-a="' + data + '" class="zy-list-icon-delete" title="删除"></i>';
                    }
                }
            },
            {
                data: 'id',
                render: function (data,type,row) {
                    return '<i data-id="' + data + '" data-type ="' + row.relativePositionRelation + '" class="zy-details-icon" title="查看详情"></i>';
                }
            }
        ]
    });

    //列表查询
    $search.click(function(){
        if(DTTABLE){
            DTTABLE.draw();
        }
    });

    //新建
    $create.click(function(){
        var id = '';
        var type = 'AEPipeLine';
        window.location.href = ambientApi.url_ae_addEdit(id,type);
    });

    //导出
    $export.click(function(){
        var param = filter.getExport;
        if(param instanceof Function){
            param = param();
        }
        $(this).EXPT({
            type: 'name',
            name: param.pipeLineName + '第三方管道设施信息表',
            param: param,
            server: DyyBase.root_path + '/ae/aePipeLine/export.json'
        });
    });

    // 审核
    $table.on('click', 'i.zy-list-icon-examine', function () {
        var $this = $(this);
        var id = $this.attr('data-a');
        var type = 'AEPipeLine';
        window.location.href = ambientApi.url_ae_examine(id,type);
    });

    // 编辑
    $table.on('click', 'i.zy-record-add-icon', function () {
        var $this = $(this);
        var id = $this.attr('data-a');
        var type = 'AEPipeLine';
        if(!($this.hasClass('disabled'))){
            window.location.href = ambientApi.url_ae_addEdit(id,type);
        }
    });

    // 删除
    $table.on('click', 'i.zy-list-icon-delete', function () {
        var $this = $(this);
        var id = $this.attr('data-a');
        if(!($this.hasClass('disabled'))){
            $this.MSG({
                type: 'confirm',
                tag: 'no',
                height: 230,
                content: '您确认要删除此条数据吗?',
                yesBtnTxt: '删除',
                yesBtnAct: function(){
                    DyyBase.ajax(ambientApi.url_ae_pipe_delete(), {id:id}, function(dataSet){
                        if(dataSet.bool){
                            $(this).MSG({
                                type: 'success',
                                height: 300,
                                content: '删除成功',
                                yesBtnTxt: '确认',
                                yesBtnAct: function(){
                                    if(DTTABLE){
                                        DTTABLE.draw();
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }
    });

    // 详情
    $table.on('click', 'i.zy-details-icon', function () {
        var $this = $(this);
        var Id = $this.attr('data-id');
        var type = $this.attr('data-type');
        DyyBase.ajax(ambientApi.url_ae_pipe_examine(), {id:Id,type:'show'}, function(dataSet){
            if(dataSet.bool){
                if(dataSet.result.imgList){
                    var imgData = DyyBase.getImagePath(dataSet.result.imgList);
                    if(type == '交叉'){
                        DyyBase.showDivDetails($this, DTTABLE, $table, ambientTemplate['thirdCross'], imgData);
                    }else if(type == '平行'){
                        DyyBase.showDivDetails($this, DTTABLE, $table, ambientTemplate['thirdParallel'], imgData);
                    }
                }else {
                    if(type == '交叉'){
                        DyyBase.showDivDetails($this, DTTABLE, $table, ambientTemplate['thirdCross']);
                    }else if(type == '平行'){
                        DyyBase.showDivDetails($this, DTTABLE, $table, ambientTemplate['thirdParallel']);
                    }
                }
            }
        });
    });

    //表格
    $toggle_list.click(function(){
        DyyBase.toggleListMap('list');
        if(DTTABLE){
            DTTABLE.draw();
        }
    });

    //地图
    $toggle_map.click(function(){
        DyyBase.toggleListMap('map');
        var gisJsonArr;
        var input = $table.find('input[name="gisId"]');
        if(input.length>0){
            gisJsonArr = [{typeName:'PicAePipeline',gisIdArray:[]}];
            input.each(function(){
                var $this = $(this);
                var v = $this.val();
                if(v && v!='-'){
                    gisJsonArr[0].gisIdArray.push(v);
                }
            });
        }else{
            gisJsonArr = [];
        }

        //console.log(gisJsonArr);
        DyyBase.aeMap('JQ_dyMap',gisJsonArr);
    });


    //地图查询
    $mapSearch.click(function(){
        var param = filter.getSearch();
        DyyBase.ajax(ambientApi.url_ae_pipe_search(), param, function (dataset) {
            if(dataset.bool){
                var gisJsonArr;
                var returnData = dataset.result.list;
                if(returnData && returnData.length>0){
                    gisJsonArr = [{typeName:'PicAePipeline',gisIdArray:[]}];
                    for(var i=0;i<returnData.length;i++){
                        if(returnData[i].gisid){
                            gisJsonArr[0].gisIdArray.push(returnData[i].gisid);
                        }
                    }
                }else{
                    gisJsonArr = [];
                }
                //console.log(gisJsonArr);
                DyyBase.aeMap('JQ_dyMap',gisJsonArr);
            }
        });
    });

    /*var $mapIframe,dyMap,currentMap;
     var mapPath = "${gisResourcePath}/baseMapPicAe.jsp";
     function playMap(gisJsonArr){
     if(dyMap === undefined){
     $mapIframe = $('#JQ_dyMap');
     $mapIframe.attr('src','/gis/default/webmap/baseMapPicAe.jsp');
     $mapIframe.get(0).onload = function(){
     dyMap = this.contentWindow.queryMutiLayers;
     currentMap = this.contentWindow.basePipeMap.currentMap;
     dyMap(mapPath, gisJsonArr);
     }
     }else{
     dyMap(mapPath, gisJsonArr);
     }
     }*/

    /*function toggleListMap(tag){
        var $list_div = $('#JQ_toggle_list_div');
        var $map_div = $('#JQ_toggle_map_div');
        var $list_span = $('#JQ_toggle_list_span');
        var $map_span = $('#JQ_toggle_map_span');
        if(tag === 'list'){
            if(!($list_span.hasClass('active'))){
                $('div.xyf-list-search').show();  //查询表格
                $('div.xyf-map-search').hide();  //查询地图
                $('div.xyf-operate_div').show(); //左边的操作按钮，如新建/导出

                $list_span.addClass('active');
                $map_span.removeClass('active');
                $list_div.show();
                $map_div.height(0);
            }
        }else if(tag === 'map'){
            if(!($map_span.hasClass('active'))){
                $('div.xyf-list-search').hide();   //查询表格
                $('div.xyf-map-search').show();  //查询地图
                $('div.xyf-operate_div').hide();  //左边的操作按钮，如新建/导出

                $list_span.removeClass('active');
                $map_span.addClass('active');
                $list_div.hide();

                var $zy_map = $('div.zy-map');
                var $pick_div = $('div.zy-pick-line');
                var $tab_head = $('div.zy-cathodic-tab-head');
                if($zy_map.length > 0 && $pick_div.length > 0){
                    var w_h = $(window).height();
                    var p_h = $pick_div.height();
                    var h_h = w_h - p_h - 80;
                    if($tab_head.length > 0){
                        h_h = h_h - 200;
                    }
                    $map_div.height(h_h);
                }else{
                    $map_div.height(600);
                }
            }
        }
    }

    function aeMap(iframe,jsonArr,which){
        var $iframe = $('#'+iframe);
        var src = $iframe.attr('data-src');
        if(which){
            $iframe.get(0).onload = function(){
                $iframe.get(0).contentWindow.self.queryMutiLayers(jsonArr);
            };
        }else{
            $iframe.get(0).onload = function(){
                var tymode = function(){
                    var temp = '';
                    for(var i = 0, len = jsonArr.length; i < len; i++){
                        temp = jsonArr[i]['typeName'];
                    }
                    return temp;
                }();
                $iframe.get(0).contentWindow.self.typeMode = tymode;
                $iframe.get(0).contentWindow.self.queryById(jsonArr);
            };
        }
        $iframe.attr('src',src);
    }*/


})(jQuery, window, document);